package project.active;

import java.util.ArrayList;
import java.util.Date;

import model.pojo.Employee;
import project.dao.EmployeeDAO;
import project.dao.PrCompositionDAO;
import project.dao.ProjectDAO;
import project.model.PrComposition;
import project.model.PrProject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import approve.action.R;

public class Laddtester extends Activity {
	Bundle language;
	String lang="EN",database;
	PrProject pr =new PrProject();
	ProjectDAO prodao=new ProjectDAO();
	EmployeeDAO emdao=new EmployeeDAO();
	Employee emp=new Employee();
	PrCompositionDAO pcdao=new PrCompositionDAO();
	
	TextView proinfo,title;
	Button btgoprolist,btsave;
	ListView lstemcode;
	Lpopupcoder adapterEm;
	//String[] idCheck=null;
	ArrayList<Employee> emlist;
	TextView logout,username;
	public String getDatabase() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			database = extras.getString("database");
		}
		return database;
	}
	
	public String getLang() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			lang = extras.getString("lang");
		}
		return lang;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.laddtester);
		language = getIntent().getExtras().getBundle(getLang());
		
		title=(TextView)findViewById(R.id.l_tv_title_testlist);
		title.setText(language.getString("tester_team"));
		proinfo=(TextView)findViewById(R.id.l_tv_addtestproinfo);
		btgoprolist=(Button)findViewById(R.id.l_bt_addtestgoProlist);
		btsave=(Button)findViewById(R.id.l_bt_addtestsave);
		lstemcode=(ListView)findViewById(R.id.l_lv_addtestem);
		
		btgoprolist.setText(language.getString("ac_SUProjectList"));
		btsave.setText(language.getString("l_addtesterteam"));
		
		Intent i=this.getIntent();
		pr=(PrProject)i.getSerializableExtra("project");		
		emp=(Employee)i.getSerializableExtra("em");
		//idCheck=(String[])i.getSerializableExtra("lstcheck");
		
		logout=(TextView)findViewById(R.id.logout);
		username=(TextView)findViewById(R.id.username);
		logout.setText(language.getString("Logout"));
		
		logout.setOnClickListener(listenerLogout);
		username.setText(emp.getUserName());
		
		emlist=loadEmp();
		
		proinfo.setText(pr.getIdProject()+" - "+pr.getNameProject());
		btgoprolist.setOnClickListener(goprolist);
		btsave.setOnClickListener(savecoder);
		
		if(emlist.size()>0){
			adapterEm = new Lpopupcoder(this,emlist);
			lstemcode.setAdapter(adapterEm);
		}
		

	}
	
//	private OnItemClickListener oncheck = new OnItemClickListener() {
//
//		@Override
//		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
//				long arg3) {
//			// TODO Auto-generated method stubpo
//			Employee item= (Employee) arg0.getItemAtPosition(position);
////			System.out.println("kkkkk: "+ item.getIdemployee());
////			Intent i= new Intent(Laddcoder.this,Laddcoder.class);
////			i.putExtra("lang", getLang());
////			i.putExtra("database", getDatabase());
////			i.putExtra("em",emp);
////			i.putExtra("project", pr);
////			startActivity(i);
//			
//			if(idCheck!=null){
//				int kq=0; 
//				int vt=0;
//				for(int i=0;i<idCheck.length;i++){
//					if(idCheck[i]!=null){
//						if(idCheck[i].equals(item.getIdemployee())){
//							idCheck[i]=null;
//							kq++;
//							break;
//						}
//					}else{
//						vt=i;
//					}
//				}
//				
//				if(kq==0){
//					if(vt!=0){
//						idCheck[vt]=item.getIdemployee();
//					}
//				}
//			}else{
//				idCheck= new String[emlist.size()];
//				idCheck[0]=item.getIdemployee();
//			}
//		}
//		
//	};
	
	private OnClickListener savecoder= new OnClickListener() {
		
	
		public void onClick(View v) {
			// TODO Auto-generated method stub
			//System.out.println("222222");
//			idCheck=adapterEm.getIdCheck();
//			if(idCheck!=null){
//				//System.out.println("11111");
//				for(int i=0;i<idCheck.length;i++){
//					if(idCheck[i]!=null){
//						PrComposition pc=new PrComposition();
//						Date d=new Date();
//						pc.setIdComposition(""+d.getTime());
//						pc.setIdEmployee(idCheck[i]);
//						pc.setIdProject(pr.getIdProject());
//						if(pcdao.checkExitInsertTester(getDatabase(), pc)==false){
//							pcdao.insertTester(getDatabase(), pc);
//						}
//						
//					}
//					
//				}
//				
//				Intent i= new Intent(Laddtester.this,Laddmemmenu.class);
//				i.putExtras(getIntent().getExtras());
//				i.putExtra("lang", getLang());
//				i.putExtra("database", getDatabase());
//				i.putExtra("project", pr);
//				i.putExtra("em",emp);
//				
//				startActivity(i);
//			}
			
			int k=adapterEm.getK();
			
			if(k>0){
				Date d;
				ArrayList<String> lst=adapterEm.getLstcheck();
				String sin="";
				for(int i=0;i<lst.size();i++){
					
						d=new Date();
						if(pcdao.checkExitInsertTesterMulti(getDatabase(), pr.getIdProject(),lst.get(i))==false){
							if(sin.equals("")){
								sin="('"+d.getTime()+"','"+pr.getIdProject()+"','"+lst.get(i)+"',0,0)";
							}else{
								sin+=",('"+d.getTime()+"','"+pr.getIdProject()+"','"+lst.get(i)+"',0,0)";
							}
						}
						
				}
				
				if(!sin.equals("")){
					String sql="insert into pr_composition (IDComposition,IDProject,IDEmployee,Position,Status) values"+sin+"";
					if(pcdao.insertComMulti(getDatabase(), sql)>0)
						System.out.println("ok");
					else{
						System.out.println("not insert");
					}
				}
				
				Intent i= new Intent(Laddtester.this,Laddmemmenu.class);
				i.putExtras(getIntent().getExtras());
				i.putExtra("lang", getLang());
				i.putExtra("database", getDatabase());
				i.putExtra("project", pr);
				i.putExtra("em",emp);
				
				startActivity(i);
			}
			
			
		}
	};
	
	public ArrayList<Employee> loadEmp(){
		ArrayList<Employee> lst=new ArrayList<Employee>();
		lst=emdao.getAllEmployee(getDatabase());
		return lst;
	}
	
	private OnClickListener goprolist= new OnClickListener() {
		
	
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent i= new Intent(Laddtester.this,Projectlist.class);
			i.putExtras(getIntent().getExtras());
			i.putExtra("lang", getLang());
			i.putExtra("database", getDatabase());
			i.putExtra("em",emp);
			
			startActivity(i);
		}
	};
	
	private OnClickListener listenerLogout=new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent i=new Intent(Laddtester.this, LogoutActivity.class);
			i.putExtra("database", getDatabase());
			startActivity(i);
		}
	};
}
